// 文件路径: com/rainkaze/lifeharbor/tools/weather/adapter/DailyForecastAdapter.java
package com.rainkaze.lifeharbor.tools.weather.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.rainkaze.lifeharbor.R;
import com.rainkaze.lifeharbor.tools.weather.model.DailyTemperatureValue;
import com.rainkaze.lifeharbor.tools.weather.model.DailyWeatherValue;
import com.rainkaze.lifeharbor.tools.weather.model.SunRiseSetValue;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Map;

public class DailyForecastAdapter extends RecyclerView.Adapter<DailyForecastAdapter.ViewHolder> {

    private static final String TAG = "DailyForecastAdapter";
    private List<DailyForecastItem> dailyItems;
    private Context context;
    private Map<String, String> weatherCodeToDescriptionMap;
    private Map<String, Integer> weatherCodeToIconMap;
    private String tempUnit;


    // 内部类，用于整合每日的数据
    public static class DailyForecastItem {
        public String dateDisplay; // 例如 "周三\n05/12" 或 "明天"
        public String tempHigh;
        public String tempLow;
        public String weatherDescription;
        public String dayWeatherCode; // 用于图标

        public DailyForecastItem(String dateDisplay, String tempHigh, String tempLow, String weatherDescription, String dayWeatherCode) {
            this.dateDisplay = dateDisplay;
            this.tempHigh = tempHigh;
            this.tempLow = tempLow;
            this.weatherDescription = weatherDescription;
            this.dayWeatherCode = dayWeatherCode;
        }
    }


    public DailyForecastAdapter(Context context,
                                List<DailyForecastItem> dailyItems,
                                Map<String, String> weatherCodeToDescriptionMap,
                                Map<String, Integer> weatherCodeToIconMap,
                                String tempUnit) {
        this.context = context;
        this.dailyItems = dailyItems != null ? dailyItems : new ArrayList<>();
        this.weatherCodeToDescriptionMap = weatherCodeToDescriptionMap;
        this.weatherCodeToIconMap = weatherCodeToIconMap;
        this.tempUnit = tempUnit != null ? tempUnit : "°C";
    }

    public void updateData(List<DailyForecastItem> newDailyItems, String newTempUnit) {
        this.dailyItems.clear();
        if (newDailyItems != null) {
            this.dailyItems.addAll(newDailyItems);
        }
        this.tempUnit = newTempUnit != null ? newTempUnit : "°C";
        notifyDataSetChanged();
        Log.d(TAG, "Daily data updated. Item count: " + this.dailyItems.size());
    }


    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_daily_weather, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        DailyForecastItem item = dailyItems.get(position);

        holder.textViewDailyDate.setText(item.dateDisplay);
        holder.textViewDailyTempHigh.setText(String.format("%s%s", item.tempHigh, tempUnit));
        holder.textViewDailyTempLow.setText(String.format("%s%s", item.tempLow, tempUnit));
        holder.textViewDailyWeatherDescription.setText(item.weatherDescription);
        holder.imageViewDailyWeatherIcon.setImageResource(getWeatherIconResId(item.dayWeatherCode));
    }

    @Override
    public int getItemCount() {
        return dailyItems.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView textViewDailyDate;
        ImageView imageViewDailyWeatherIcon;
        TextView textViewDailyTempHigh;
        TextView textViewDailyTempLow;
        TextView textViewDailyWeatherDescription;

        ViewHolder(View itemView) {
            super(itemView);
            textViewDailyDate = itemView.findViewById(R.id.textViewDailyDate);
            imageViewDailyWeatherIcon = itemView.findViewById(R.id.imageViewDailyWeatherIcon);
            textViewDailyTempHigh = itemView.findViewById(R.id.textViewDailyTempHigh);
            textViewDailyTempLow = itemView.findViewById(R.id.textViewDailyTempLow);
            textViewDailyWeatherDescription = itemView.findViewById(R.id.textViewDailyWeatherDescription);
        }
    }

    private String getWeatherDescription(String code) {
        if (code == null || weatherCodeToDescriptionMap == null) return "未知";
        return weatherCodeToDescriptionMap.getOrDefault(code, "代码 " + code);
    }

    private int getWeatherIconResId(String code) {
        if (code == null || weatherCodeToIconMap == null) return R.drawable.unknwn_weather; // 确保你有这个默认图标
        return weatherCodeToIconMap.getOrDefault(code, R.drawable.unknwn_weather);
    }
}
